/*
 * @Author: gk 
 *  系统初始化
 * @Date: 2018-03-15 14:44:57 
 * @Last Modified by: csy
 * @Last Modified time: 2019-05-28 17:21:00
 */
layui.config({
}).use(['element', 'layer', 'form','element','laydate', 'zlConfig', 'zlContext','upload'], function (exports) {
    var $ = layui.$
        , zlContext = layui.zlContext
        , form = layui.form
        , element = layui.element
        , laydate = layui.laydate
        , upload = layui.upload
        , zlConfig = layui.zlConfig
        , statusName = zlConfig.global.result.statusName
        , msgName = zlConfig.global.result.msgName
        , dataName = zlConfig.global.result.dataName
        , ERROR = zlConfig.global.result.errorState
        , OK = zlConfig.global.result.okState;
    $(window).on('resize', function () {
        $('.docking-left').height($(window).height() - 10);
        $('.docking-fill').height($(window).height() - 10);
    }).resize();

    zlContext.post(layui.cache['contentPath'] + '/core/security/license/machinecode', {}, function (result) {
        if (result[statusName] == OK) {
            var datas = result[dataName];
            $("#frmGridLicense input[name='machinecode']").val(datas['machinecode']);
            $("#frmGridLicense input[name='licenseStatus']").val(datas['licenseStatus']);
            if(datas['hasLicense'] && "1" == datas['hasLicense']){
                if(datas['hasSysInit'] && "1" == datas['hasSysInit']){
                    $("#btnGoTo").text("返回登录页");
                    $('input[name="sysInitFlag"]').val("1");
                }else{
                    $("#btnGoTo").text("返回初始化界面");
                    $('input[name="sysInitFlag"]').val("0");
                }
                $("#btnGoTo").css('display','inline-block');
            }
        }
        else {
            zlContext.errorMsg(result[msgName]);
        }
    });

    var uploadInst = upload.render({
        elem: '#uploadLicense',
        accept: 'file', //所有的文件
        exts: 'lic', //cer文件
        url: layui.cache['contentPath'] + '/core/security/license/uploadlicense',
        done: function (res) {
            if (res[statusName] != OK) {
                return zlContext.errorMsg(res[msgName]);
            } else {
                zlContext.successMsg(res[msgName]);
                $("#frmGridLicense input[name='licenseStatus']").val(res[msgName]);
                var datas = res[dataName];
                if(datas){
                	if(datas['hasSysInit'] && "1" == datas['hasSysInit']){
                        $("#btnGoTo").text("返回登录页");
                        $('input[name="sysInitFlag"]').val("1");
                    }else{
                        $("#btnGoTo").text("返回初始化界面");
                        $('input[name="sysInitFlag"]').val("0");
                    }
                	$("#btnGoTo").css('display','inline-block');
                }
            }
        },
        error: function () {
            layer.msg('上传失败');
        }
    });

    $('#btnGoTo').on('click', function () {
        var sysInitFlag = $('input[name="sysInitFlag"]').val();
        if("1" == sysInitFlag){
            window.location.href=layui.cache['contentPath'] + '/' + $('input[name="orgcode"]').val();
        }else{
            window.location.href=layui.cache['contentPath'] + '/manager/init/';
        }
        return false;
    });

    /*
    laydate.render({
        elem: '#licenseExpire'
        ,type: 'datetime'
        ,min: 0
        ,format: 'yyyy-MM-dd HH:mm:ss'
    });

    form.on('radio(licensePermanence)', function(data){
        if(1 == data.value){
            //永久
            $("#licenseExpire").val('');
            $("#licenseExpire").attr("disabled","disabled");
            $("#licenseExpire").attr("lay-verify","");
        }else{
            $("#licenseExpire").removeAttr("disabled");
            $("#licenseExpire").attr("lay-verify","require");
        }
    }); 

    form.verify({
        verifyMachineCode: function (value, item) { //value：表单的值、item：表单的DOM对象
          if (value == "") {
            return '机器码不能为空';
          }
          if(!new RegExp("^[A-Z0-9]{4}(-[A-Z0-9]{4}){7}").test(value)){
            return '机器码格式错误';
          }
        }
    });

    //提交
	form.on('submit(generateLicense)', function (frm) {
        var machinecode = $("#frmGenerateLicense input[name='machinecode']").val();
        var licensePermanence = $("#frmGenerateLicense input[name='licensePermanence']:checked ").val();
        var licenseExpire = $("#frmGenerateLicense input[name='licenseExpire']").val();

        var url = layui.cache['contentPath'] + '/core/security/license/generatelicense?machinecode=' + machinecode + 
            '&licensePermanence=' + licensePermanence + '&licenseExpire=' + licenseExpire;
        if ($("#ifdownload").length == 0) {
            $("body").append('<iframe id=ifdownload style="display:none"></iframe>');
        }
        $("#ifdownload").attr("src", url);
        var iframe = document.getElementById('ifdownload');
        iframe.onload = iframe.onreadystatechange = function iframeload() {
            if (!iframe.readyState || iframe.readyState == "complete") {
                var iframeText = document.getElementById('ifdownload').contentWindow.document;
                iframeText = $(iframeText).text();
                if(iframeText){
                    var obj = JSON.parse(iframeText);
                    zlContext.errorMsg(obj.msg);
                }else{
                    zlContext.successMsg('生成license授权文件');
                }
            }
        };
        
		return false;
	});
    */
});